package cn.kgc.education.controller;

import cn.kgc.education.entity.Student;
import cn.kgc.education.response.ResponseResult;
import cn.kgc.education.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author 课工场
 * @date 2024/9/10
 * @description 学生模块  请求处理类
 */
@RestController
@RequestMapping("student")
public class StudentController {

    @Resource
    private StudentService studentService;


    // 学生数据的分页
    @RequestMapping("page")
    public ResponseResult studentPage(Integer page, Integer limit, String stuName, String major) {

        Page<Student> studentPage = new Page<>(page, limit);


        // where   stu_name = ?  and  major = ?
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.like(StringUtils.isNotBlank(stuName), "stu_name", stuName)
                .eq(StringUtils.isNotBlank(major), "major", major);

        // 分页查询   +  搜索
        studentService.page(studentPage, studentQueryWrapper);


        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setCount(studentPage.getTotal());
        responseResult.setData(studentPage.getRecords());

        return responseResult;

    }


    // 学生数据删除
    @RequestMapping("delete")
    public ResponseResult deleteStudent(String ids) {

        String[] strIds = ids.split(",");
        ArrayList<Integer> intIds = new ArrayList<>();
        for (String strId : strIds) {
            intIds.add(Integer.valueOf(strId));
        }

        studentService.removeBatchByIds(intIds);


        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("删除成功");

        return responseResult;
    }


    //  添加 编辑学生信息
    @RequestMapping("addOrUpdate")
    public ResponseResult addOrUpdateStudent(Student student) {

        ResponseResult responseResult = new ResponseResult();

        // 判定添加业务 学号是否重复
        if (student.getId()==null){
            String stuNo = student.getStuNo();
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.eq("stu_no",stuNo);
            Student one = studentService.getOne(studentQueryWrapper);
            if (ObjectUtils.isNotNull(one)){
                    responseResult.setCode(110);
                    responseResult.setMsg("学号已存在");
                    return  responseResult;
            }

            student.setCreateTime(new Date());
        }

        studentService.saveOrUpdate(student);

        responseResult.setCode(0);
        responseResult.setMsg("操作成功");
        return  responseResult;


    }


}

